/*
 * CBIO.cpp
 *
 *  Created on: 2009-5-17
 *      Author: thh
 */

#include "CBIO.h"

#include <openssl/evp.h>
#include <openssl/ssl.h>

CBIO::CBIO()
{
	this->bio = NULL;
}
CBIO::CBIO(CMETHOD method)
{
    this->bio = BIO_new(this->getMethod(method));
}

CBIO::~CBIO()
{
	if(this->bio!=NULL)
		BIO_free(this->bio);
}

CBIO::operator BIO*() const
{
    return this->bio;
}

BIO_METHOD *CBIO::getMethod(CMETHOD method)
{
    switch(method.method)
    {
        case CMETHOD::F_BASE64:
            return BIO_f_base64();
        case CMETHOD::F_BUFFER:
            return BIO_f_buffer();
        case CMETHOD::F_CIPHER:
            return BIO_f_cipher();
        case CMETHOD::F_MD:
            return BIO_f_md();
        case CMETHOD::F_NULL:
            return BIO_f_null();
        case CMETHOD::F_SSL:
            return BIO_f_ssl();
        case CMETHOD::S_ACCEPT:
            return BIO_s_accept();
        case CMETHOD::S_BIO:
            return BIO_s_bio();
        case CMETHOD::S_CONNECT:
            return BIO_s_connect();
        case CMETHOD::S_FD:
            return BIO_s_fd();
        case CMETHOD::S_FILE:
            return BIO_s_file();
        case CMETHOD::S_MEM:
            return BIO_s_mem();
    }
    return NULL;
}
